package com.zhm.demo.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class ExcelTest {

    @Data
    public static class A {

        @ExcelProperty("adcode")
        private String adcode;
    }


    public static void testGEO() {
        File file = new File("D:\\文档\\地址库\\AMap_adcode_citycode.xlsx");
        List<String> adcodeList = new ArrayList<>();
        EasyExcel.read(file, A.class, new AnalysisEventListener<A>() {
            @Override
            public void invoke(A o, AnalysisContext analysisContext) {
                System.out.println(o);
                adcodeList.add(o.getAdcode());
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//                adcodeList.forEach(adcode -> {
//                    File file1 = new File(System.getProperty("user.dir") + File.separator + "geoResource" + File.separator + adcode + "_full.json");
//                    if(!file1.exists()) {
//                        try {
//                            HcHttpClientHelper.defaultClient().get("https://geo.datav.aliyun.com/areas_v3/bound/" + adcode + "_full.json").exec().then(hcHttpClientRes -> {
//                                if (200 == hcHttpClientRes.hcHttpClientDataRes.getCode()) {
//                                    hcHttpClientRes.toFile(file1);
//                                } else {
//                                    System.out.println(adcode + " res code = " + hcHttpClientRes.hcHttpClientDataRes.getCode());
//                                }
//                            });
//                        } catch (IOException e) {
//                            throw new RuntimeException(e);
//                        }
//                    }else {
//                        System.out.println(adcode + " file exist");
//                    }
//                    try {
//                        Thread.sleep(10000);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                });

            }
        }).sheet("sheet1").doRead();
        ;

    }

    /**
     * 销售明细
     */
    @Data
    public static class SaleDetail {

        @ExcelProperty("销售组织")
        private String saleOrgCode;

        @ExcelProperty("销售组织描述")
        private String saleOrgDesc;

        @ExcelProperty("分销渠道")
        private String saleChannelCode;

        @ExcelProperty("分销渠道描述")
        private String saleChannelDesc;

        @ExcelProperty("产品组")
        private String productGroupCode;

        @ExcelProperty("产品组描述")
        private String productGroupDesc;

        @ExcelProperty("订单类型")
        private String orderTypeCode;

        @ExcelProperty("订单类型描述")
        private String orderTypeDesc;
    }


    public static void test01() throws IOException {

//        testGEO();
        log.info("开始解析");
        File file = new File("C:\\Users\\zhanghuaming\\Downloads\\3.3.xlsx");
        if (!file.exists()) {
            log.error("文件不存在");
        } else {
            log.info("文件存在");
        }

//        List<SaleDetail> userList = EasyExcel.read(file)
//                .head(SaleDetail.class)
//                .sheet("Sheet1")
//                .doReadSync();
//
//        System.out.println(userList);


        EasyExcel.read(file, SaleDetail.class, new AnalysisEventListener<SaleDetail>() {


            @Override
            public void invoke(SaleDetail o, AnalysisContext analysisContext) {
                System.out.println(o);
            }


            @Override
            public void onException(Exception exception, AnalysisContext context) throws Exception {
                throw exception;
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("解析完成");
                log.info("解析完成");
            }
        }).sheet().doRead();
    }


    public static void testWrite() throws IOException {
        String path = System.getProperty("user.dir");
        System.out.println(path);
        List<UserDO> userList = new ArrayList<>();
        UserDO userDO = new UserDO();
        userDO.setId(1L);
        userDO.setBirthday(new Date());
        userDO.setGender(21);
        userDO.setPassword("1321312e");
        userDO.setHeight(2.0);
        userDO.setPhone("137156232342");
        userList.add(userDO);
        File file = new File(path + File.separator + "a.xlsx");
        file.createNewFile();
        EasyExcel.write(new FileOutputStream(file))
                .head(UserDO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("用户列表")
                .doWrite(userList);
    }

    public static void testRead2() {
//        File file = new File("D:\\文档\\地址库\\AMap_adcode_citycode.xlsx");
        File file = new File("D:\\Users\\zhanghm\\Desktop\\标准报价单（发送客户需导出PDF格式）.xlsx");
        EasyExcel.read(file, QuotationDo.class, new AnalysisEventListener<QuotationDo>() {
                    @Override
                    public void invoke(QuotationDo o, AnalysisContext analysisContext) {
                        System.out.println(o);
                    }

                    @Override
                    public void onException(Exception exception, AnalysisContext context) throws Exception {
                        throw exception;
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        System.out.println("解析完成");
                        log.info("解析完成");
                    }

                })
                .headRowNumber(5)
                .sheet("货架产品（COTS）")
                .doRead();


    }

    public static void testWrite2() throws IOException {
        List saveList = new ArrayList<>();

        File file = new File("D:\\Users\\zhanghm\\Desktop\\标准报价单（发送客户需导出PDF格式）.xlsx");
        EasyExcel.read(file, new ReadListener() {
            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {
                saveList.add(o);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                QuotationDo quotationDo = new QuotationDo();
                quotationDo.setNo("10");
                try {
                    quotationDo.setImageUrl(new URL("file:/D:\\Users\\zhanghm\\Desktop\\2.jpg"));
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
                quotationDo.setProductModel("测试");
                quotationDo.setDescription("描述");
                quotationDo.setGp20Quantity("1");
                quotationDo.setHq40Quantity("2");
                quotationDo.setDomesticPrice("3");
                quotationDo.setFobPrice("4");
                quotationDo.setChinaPrice("5");
                saveList.add(quotationDo);

                try {
                    EasyExcel.write(new FileOutputStream(new File("D:\\Users\\zhanghm\\Desktop\\123.xlsx")))
    //                .head(QuotationDo.class)
                            .excelType(ExcelTypeEnum.XLSX)
                            .sheet("货架产品（COTS）")
                            .doWrite(saveList);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
        }).headRowNumber(0).sheet("货架产品（COTS）").doRead();


    }

    public static void main(String[] args) throws IOException {
        testWrite2();
    }
}
